import { Component, OnInit, Input } from '@angular/core';
import { NzModalRef } from 'ng-zorro-antd';

@Component({
  selector: 'bz-select-field-modal',
  templateUrl: './select-field-modal.component.html',
  styles: [`
    .label-item{
      margin-bottom: 16px;
    }
  `]
})
export class SelectFieldModalComponent implements OnInit {

  @Input() fields: any;

  showChooseLeastOneDanger = false;

  constructor(public nzModalRef: NzModalRef) { }

  ngOnInit() {
    this.fields.forEach((field) => {
      field.__bzShow = field.getShow();
    });
  }

  /**
   * 联动控制是否禁用
   * @param bzFreezeShow
   * @description 根据传入的bzName对应的复选框禁用状态去动态设置当前复选框是否禁用，支持多个bzName，以空格分隔
   */
  dynamicFreezeShow(bzFreezeShow, i) {
    if (bzFreezeShow === false || bzFreezeShow === 'false') {
      return false;
    }
    if (bzFreezeShow === true || bzFreezeShow === 'true') {
      return true;
    }
    if (bzFreezeShow.includes(' ')) {
      const _bzFreezeShowList = bzFreezeShow.split(' ');
      const _bzFreezeShow = _bzFreezeShowList.some((field) => {
        return !this.fields.find((item) => field === item.bzName).__bzShow;
      });
      if (_bzFreezeShow) {
        this.fields[i].__bzShow = false;
      }
      return _bzFreezeShow;
    }
    return false;
  }

  save() {
    const valid = this.fields.some((field) => field.__bzShow);
    if (valid) {
      this.fields.forEach((field) => {
        field.customShow = field.__bzShow;
      });
      this.nzModalRef.triggerOk();
    } else {
      this.showChooseLeastOneDanger = true;
    }
  }
}
